package com.yunfei.commonbase.hdutil.conreoller;



import com.yunfei.commonbase.hdutil.CephUtil;
import com.yunfei.commonbase.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ceph")
public class CephController {

    @ApiOperation(value = "ceph上传文件",notes = "<pre>\n" +
            "参数说明：\n" +
            "url:上传文件的路径  以/结尾\n" +
            "出参说明：\n" +
            "返回文件的下载路径\n" +
            "</pre>")
    @RequestMapping(value = "/uploaddoc", method = RequestMethod.POST)
    public Result tool(@RequestParam("file") MultipartFile file, @RequestParam("url") String url) {
        String url_ = CephUtil.uploaddoc(file,url);
        Map<String, String> map = new HashMap<>();
        map.put("url", url_);
        return new Result(map);
    }

    @ApiOperation(value = "ceph下载文件",notes = "<pre>\n" +
            "参数说明：\n" +
            "url:下载文件的路径（上传时返回的路径信息）\n" +
            "</pre>")
    @RequestMapping(value = "/downloaddoc", method = RequestMethod.GET)
    public Result downloaddoc(@RequestParam("url") String url, HttpServletResponse res) {
        CephUtil.download(url,res);
        System.out.println("success");
        return new Result("success");
    }


    @ApiOperation(value = "ceph删除指定文件",notes = "<pre>\n" +
            "参数说明：\n" +
            "url:要删除的文件路径信息（上传时返回的路径信息）\n" +
            "</pre>")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestParam("url") String url) {
        try {
            File file = new File(url);
            file.delete();
            return new Result("删除成功");
        }catch (Exception e){
            return new Result("1","删除失败");
        }
    }


    @ApiOperation(value = "ceph递归删除文件夹下所有文件及其子文件",notes = "<pre>\n" +
            "参数说明：\n" +
            "url:要删除的文件路径信息(如果时文件夹  会删除子文件夹的文件)\n" +
            "</pre>")
    @RequestMapping(value = "/recursionDelete", method = RequestMethod.POST)
    public Result recursionDelete(@RequestParam("url") String url) {
        try {
            File file = new File(url);
            CephUtil.delFile(file);
            return new Result("删除成功");
        }catch (Exception e){
            return new Result("1","删除失败");
        }
    }


    @ApiOperation(value = "ceph查询文件夹下所有文件",notes = "<pre>\n" +
            "参数说明：\n" +
            "url:查询文件夹下所有文件路径（包含子文件夹）\n" +
            "</pre>")
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public Result findAll(@RequestParam("url") String url) {
        try {
            List<File> files = new ArrayList<>();
            CephUtil.readFile(url,files);
            List<String> filesUrl = new ArrayList<>();
            for (File f1 : files) {
                filesUrl.add(f1.getAbsolutePath());
            }
            return new Result("删除成功");
        }catch (Exception e){
            return new Result("1","删除失败");
        }
    }
}
